﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.RAL.Access;
using Empires.CRL.DataContract;
using Empires.RAL.ORM;
using Empires.RAL.ORM.CompanyData;

namespace Empires.RAL.Pump
{
    public class CompanyDownloadDataPump : ICompanyDownloadAccess
    {
        public string GetDownloadFileName(int id, string copname)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = (from c in ctx.tb_download
                              where c.c_i_id == id && c.c_vc_cpname == copname && !c.c_b_isDelete
                              select c.c_nvc_url).FirstOrDefault();
                if (String.IsNullOrEmpty(result))
                {
                    return "";
                }
                return result;
            }
        }

        public int GetDownloadFilesItemCount(string copname, int type)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                return ctx.tb_download
                    .Where(c => c.c_vc_cpname == copname && c.c_i_type == type && !c.c_b_isDelete)
                    .Count();
            }
        }
        
        public List<DefaultDataPack> GetDownloadFiles(string copname, int type,int page,int take)
        {
            List<DefaultDataPack> list = new List<DefaultDataPack>();
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = (from c in ctx.tb_download
                              where c.c_vc_cpname == copname && c.c_i_type == type && !c.c_b_isDelete
                              orderby c.c_i_id descending
                              select new
                              {
                                  c.c_dt_uploadDate,
                                  c.c_i_hit,
                                  c.c_i_id,
                                  c.c_nvc_title
                              })
                              .Skip(page * take)
                              .Take(take).ToList();
                if (result != null)
                {
                    list = result.Select(c =>
                        new DefaultDataPack()
                        {
                            DATE = c.c_dt_uploadDate.ToLongDateString(),
                            HIT = c.c_i_hit,
                            ID = c.c_i_id,
                            TITLE = c.c_nvc_title
                        }).ToList();
                }
                else
                {
                    list.Add(new DefaultDataPack());
                }
                return list;
            }
        }
        
        public int DeleteDownloadFile(string copname, int fid)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var item = ctx.tb_download.FirstOrDefault(c=>c.c_vc_cpname == copname && c.c_i_id == fid);
                if (item != null)
                {
                    item.c_b_isDelete = true;
                    return ctx.SaveChanges();
                }
                return 0;
            }
        }
        
        public bool DownloadAddItem(string id, DownloadDataPack datapack)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                ctx.tb_download.AddObject(
                    new tb_download()
                    {
                        c_dt_uploadDate = DateTime.Now,
                        c_b_isDelete = false,
                        c_i_hit = datapack.Hit,
                        c_i_type = 0,
                        c_nvc_author = datapack.Author,
                        c_nvc_descript = string.Empty,
                        c_nvc_title = datapack.Title,
                        c_nvc_url = datapack.URL,
                        c_vc_cpname = id
                    });
                return ctx.SaveChanges() == 1;
            }
        }
    }
}
